package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.service.TemplatesService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("item")
public class ItemController {
    private Logger logger = LoggerFactory.getLogger(ItemController.class);
    @Reference
    private ItemService itemService;
    @Reference
    private TemplatesService templatesService;

    @RequestMapping("toItem")
    public String toItem(){
        return "admin/item_cat";
    }

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(@RequestParam(defaultValue = "1")Integer number,@RequestParam(defaultValue = "10")Integer pageSize
            ,@RequestParam(defaultValue = "0")Long pid){
        try {
            PageBean<TbItemCat> b = itemService.getinfo(number,pageSize,pid);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),b);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toAdd")
    @ResponseBody
    public dataResult toAdd(Long pid){
        try {
            /*String pname = itemService.toAdd(pid);*/
            List<TbTypeTemplate> list = templatesService.getAll();
            /*Map<String,Object> map = new HashMap<>();
            map.put("pname",pname);
            map.put("list",list);*/
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),list);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("addinfo")
    @ResponseBody
    public dataResult addinfo(TbItemCat t,Long pid){
        try {
            t.setParentId(pid);
            Integer i = itemService.addinfo(t);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public dataResult deleteAll(@RequestParam("ids[]")Long[] ids){
        try {
            Integer i = itemService.deleteAll(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public dataResult toUpdate(Long id){
        try {
            TbItemCat item  = itemService.toUpdate(id);
            List<TbTypeTemplate> list = templatesService.getAll();
            Map<String,Object> map = new HashMap<>();
            map.put("list",list);
            map.put("item",item);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),map);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("updateinfo")
    @ResponseBody
    public dataResult updateinfo(TbItemCat t,Long pid){
        try {
            t.setParentId(pid);
            Integer i = itemService.updateinfo(t);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }
}
